import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/api/req/user/req_user.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/other/message_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class MessageController extends BaseGetPageController {

  List<MessageModel> messageList = [];

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqOther.userMessageList(
      pageNum: page,
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        if (response.data['data']['hasNextPage'] == false) {
          RefreshExtension.onSuccess(controller, refresh, true); 
        } else {
          RefreshExtension.onSuccess(controller, refresh, false);
        }

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          messageList.clear();
          update();
        }

        for (int i = 0; i < response.data['data']['foUserMessageVOList'].length; i++) {
          messageList.add(MessageModel.fromJson(response.data['data']['foUserMessageVOList'][i]));
        }

        showSuccess(messageList);
        update();
      } else if (response.data['code'] == 401) {
        RefreshExtension.onError(controller, refresh);
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }

  // 用户单条消息已读
  readMessage(int index) async {
    var response = await ReqUser.readMessage(
      messageId: '${messageList[index].messageId ?? ''}',
    );
    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        messageList[index].messageNum = 0;
        update();
        eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_SIGNLE_MESSAFE_NUM));
      }
    }
  }

  // 用户全部已读
  readAll() async {
    try {
      EasyLoading.show();

      var resp = await ReqUser.readAll();
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          requestData(controller!);

          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_ALL_MESSAGE_NUM));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }


}